<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
class Index extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        //音乐
        $mu = Db::name('music')->where('status','0')->select(); 
        // var_dump( $mu[0]['mmage']);die;
        //广告
        $advi = Db::name('Adverti')->where('status','0')->select(); 
        //游戏
        $game = Db::name('game')->where('status','0')->select(); 
        // 轮播图
        $list =  Db::table('carousel')
        ->alias('c')
        ->join('article a','c.aid = a.aid')
        ->where('c.display = 1')
        ->select();
        // var_dump($list);die;
        //内容
        $article = Db::table('article a')
        ->join('user_self u','u.uid = a.uid')
        ->where('dispaly = 1')
        ->order('regtime desc')
        ->select();
        // var_dump($article);die;
       
        //热文
        $paper = Db::table('article a')
        ->join('user_self u','u.uid = a.uid')
        ->order('regtime desc')
        ->where('hot = 1 and dispaly = 1')
        ->limit(5)
        ->select();
       //登录分类
       if (session('uid','','think')) {  
        $ass = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->order('id desc')
        ->limit(10)
        ->select();
            
        } else {
           $ass = null;
        }
        if (session('uid','','think')) {  
        $assz = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->select();
            
        } else {
           $assz = null;
        }
        //未登录栏目
        $hotss = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(9)
        ->select();
        // var_dump($list);die;
        //未登入下栏目
        $hotssz = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(10)
        ->select();

        //友情链接
        $link = Db::name('flink')->select(); 

         return view('index', [
            'article' => $article,
            'paper' => $paper,
            'list' => $list,
            'ass' => $ass,
            'assz' => $assz,
            'hotss' => $hotss,
            'link' => $link,
            'hotssz' => $hotssz,
            'advi' => $advi,
            'mu' => $mu[0]['mmage'],
            'game' => $game,
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {

        $list = Db::table('user_func')->field('tel')->select();
        $p = $request->post();
<<<<<<< HEAD
<<<<<<< HEAD


        
        if (!empty($list)) {
            foreach ($list as $k => $v) {
               $tel[]= $v['tel'];
            }

            foreach ($tel as $key => $v) {
                if ($v == $p['tel']) {
                    return $this->success('此号码已注册');            
                }
            } 
        }
        //判断密码是否一致
        // var_dump($p);die;
        if ($p['pwd'] !== $p['repwd']) {
            $this->error('两次密码不一致');
        }
<<<<<<< HEAD

=======
=======
=======
>>>>>>> eb37ef826ab0653d162d4a05c8238d99ea85e487

  if (!empty($list)){


       foreach ($list as $k => $v) {
           $tel[]= $v['tel'];
       }

        foreach ($tel as $key => $v) {
            if ($v == $p['tel']) {
                return $this->success('此号码已注册');            
            }
        }
  }
<<<<<<< HEAD
>>>>>>> 5f3922fa251ab9eb7684e7ce37e67fdc9138030f
=======
>>>>>>> eb37ef826ab0653d162d4a05c8238d99ea85e487
>>>>>>> 2d79cfa8789da2f2a423d006d49f501a7ccfc9c7
        //md5 加密
        $pwd = md5($p['pwd']);
        //内容数组
        $data = ['tel' => $p['tel'], 'pwd' => $pwd ,'regtime'=>$p['time']];
        //增加字段
        $list = Db::name('user_func')->data($data)->insert();
        //查询
        $id = Db::name('user_func')->where('tel='.$p['tel'])->find();

        if ($list > 0) {
            $da = ['uid'=>$id['uid'],'nickname'=>$p['tel']];
           Db::name('user_self')->data($da)->insert(); 
           return $this->success('添加成功', url('index/index/index'));
        } else {
            return $this->success('添加失败(ノ°ο°)ノ高能预警 ');
        }
    }
    
    /**
     * 登录
     * @return [type] [description]
     */
    public function sign(Request $request){
             $p = $request->post();

             $data = ['tel' => $p['tel'], 'pwd' => $p['pwd']];

             $list = Db::table('user_func')->where('tel',$p['tel'])->find();
            // var_dump($list);die;
<<<<<<< HEAD

            if($list == null){
                return $this->error('查无账号');
            }elseif ($list['status'] == 0) {
=======
<<<<<<< HEAD
<<<<<<< HEAD
            if ($list == null) {
                return $this->error('查无账号');
            } elseif ($list['status'] == 0) {
=======
            if($list == null){
                return $this->error('查无账号');
            }elseif ($list['status'] == 0) {
>>>>>>> 5f3922fa251ab9eb7684e7ce37e67fdc9138030f
=======
            if($list == null){
                return $this->error('查无账号');
            }elseif ($list['status'] == 0) {
>>>>>>> eb37ef826ab0653d162d4a05c8238d99ea85e487
>>>>>>> 2d79cfa8789da2f2a423d006d49f501a7ccfc9c7
                   return $this->error('账号被封请重新创建');
            }
             $name = Db::table('user_self')->where('uid',$list['uid'])->find();
             // var_dump($name['nickname']);die;

             $tel = $list['tel'];
             $pwd = $list['pwd'];
             if ($p['tel'] !== $tel) {
                return $this->error('账号错误');
             } elseif (md5($p['pwd']) !== $pwd) {
                return $this->error('密码错误');

             } {
               session('tel',$tel,'think');
               session('name',$name['nickname'],'think');
               session('uid',$name['uid'],'think');
               return $this->success('登录成功');
             }
    }

    /**
     * 退出登录
     * @return [type] [description]
     */
    public function log(){
        session('tel', null);
        session('name', null);
        $this->success('退出成功',url('index/Index/index'));
    }




    //自执行栏目
    public function asd(){
        if (session('uid','','think')) {  
        $ass = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->order('id desc')
       ->select();

        } else {
           $ass = null;
        }
        return json($ass);
    }

    
   
}
   

